require("View")

EntitiesView = class("EntitiesView", View)

EntitiesView.maxDeph = 5
EntitiesView.ignoreVariable = 
{
	properties = true,
	isEntity = true,
	name = true,
	type = true,
}

---

function EntitiesView:initialize()

	View.initialize(self)

	self.rootWindow = wx.wxTreeCtrl( 
		gView.frame, 
		wx.wxID_ANY,
        wx.wxDefaultPosition, 
        wx.wxSize(-1, -1),
        wx.wxTR_LINES_AT_ROOT + wx.wxTR_HAS_BUTTONS 
    )     
        
	self.rootId = self.rootWindow:AddRoot( "Entities" )
end

---

function EntitiesView:update()

	self.rootWindow:DeleteChildren(self.rootId)
	
	for type, entities in pairs(gField.entityTypes) do
	
		local typeId = self.rootWindow:AppendItem(self.rootId, type )
		
		for name, instance in pairs(entities) do

			self:addEntityToTree(typeId, instance, 0, "")
		end
		
	end
	
	self.rootWindow:Expand(self.rootId)
end

---

function EntitiesView:addEntityToTree(parentId, entity, depth, keyName, entityName)

	if depth < EntitiesView.maxDeph + 1 then 
	
		if not entityName then
			
			entityName = entity.name
		end
	
		local separator = ""
		if 	keyName ~= "" then
			separator = " = "
		end
		
		local locationName = ""
		
		if 
			entity:isPropertyExist("CurrentContainer") and
			entity:isPropertyCollasped("CurrentContainer")
		then
			locationName = " (in " .. entity:getCollapsedProperty("CurrentContainer").name .. ") "
		end
		
		local entityId = self.rootWindow:AppendItem(parentId, keyName .. separator .. entityName .. locationName )
		
		if 
			entity:hasProperties() and
			entity:areAllPropertiesCollapsed()
		then
			self.rootWindow:SetItemBold(entityId)
		end
	
		-- add sub entities recursively
		
		for key, value in pairs(entity) do
			
			if not EntitiesView.ignoreVariable[key] then
			 
				if 
					value and 
					(type(value) == "table") 
				then	
				
					-- entity variable is a table
							
					if value.isEntity then
					
						self:addEntityToTree(entityId, value, depth + 1, tostring(key))
					else
						self:exploreTable(entityId, value, key, depth + 1)
					end
				else
				
					-- entity variable is not a table
					
					self.rootWindow:AppendItem(entityId, 
						tostring(key) .. 
						" = " .. 
						tostring(value)
					)
				end
			end
		end
	
		-- add properties
				
		for propertiesName, propertyPossibilities in pairs(entity.properties) do
			
			local propertyId = self.rootWindow:AppendItem(entityId, propertiesName .. " = ")
			
			for possibilityKey, possibilityProbability in pairs(propertyPossibilities) do
			
				if type(possibilityKey) == "table" and possibilityKey.isEntity then
				
					-- property is an entity
					
					self:addEntityToTree(
						propertyId, 
						possibilityKey, 
						depth + 1,
						"",
						(
							possibilityKey.name .. 
							"    ( " .. 
							tostring(possibilityProbability) ..
							" )"	
						)					
					)
				else
				
					-- property is not an entity
					
					self.rootWindow:AppendItem(propertyId, 
						tostring(possibilityKey) .. 
						"    ( " .. 
						tostring(possibilityProbability) ..
						" )"
					)
				end
			end
		end

		return entityId
	end
end

---

function EntitiesView:exploreTable(parentId, table, parentKey, depth)

	if 
		(depth < EntitiesView.maxDeph) and
		(parentKey ~= "class")
	then 

		local entityId = false
		
		for key, value in pairs(table) do
		
			if value and (type(value) == "table") then
			
				if value.isEntity then
				
					if not entityId then
						entityId = self.rootWindow:AppendItem(parentId, parentKey)
					end
					
					self:addEntityToTree(entityId, value, depth + 1, "")
				else
					self:exploreTable(parentId, value, parentKey, depth + 1)
				end	
			end
		end
	end
end